package io.mvvm.nio.channel;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @program: Netty
 * @description: FileChannel 读取
 * @author: 潘
 * @create: 2021-01-17 20:58
 **/
public class FileChannelReadExample {
    public static void main(String[] args) throws Exception {
        // 创建文件输入流
        FileInputStream stream = new FileInputStream("E:\\fileName.txt");
        // 通过 stream 获取 FileChannel
        FileChannel channel = stream.getChannel();
        // 创建一个缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        // 将通道的数据读取到 buffer
        channel.read(buffer);
        // 将 buffer 转为 string
        System.out.println(new String(buffer.array()));
        channel.close();
    }
}
